package model

import (
	"github.com/lib/pq"
	"manager/model/entity"
	"manager/model/po"
)

type StoreStatus int

const (
	StoreOnLine StoreStatus = iota
	StoreOffLine
)

type StoreCreateRequest struct {
	Name string `json:"name" binding:"required"`

	Addr string `json:"addr" binding:"required"`

	Phone string `json:"phone" binding:"required"`

	Coordinate po.Point `json:"coordinate" binding:"required"`

	ManagerID int64 `json:"manager_id"`

	Images pq.StringArray `json:"images"`

	Status int `json:"status"`

	AreaID       int64  `json:"area_id"`
	ZoneCode     string `json:"zone_code"`
	CityCode     string `json:"city_code" binding:"required"`
	ProvinceCode string `json:"province_code" binding:"required"`
	CountryCode  string `json:"country_code" binding:"required"`
}

type StoreUpdateRequest struct {
	ID int64 `json:"id" binding:"required"`

	Name *string `json:"name"`

	Addr *string `json:"addr"`

	Phone *string `json:"phone"`

	Coordinate *po.Point `json:"coordinate"`

	ManagerID *int64 `json:"manager_id"`

	Images *pq.StringArray `json:"images"`

	Status *int `json:"status"`

	AreaID       *int64  `json:"area_id"`
	ZoneCode     *string `json:"zone_code"`
	CityCode     *string `json:"city_code" `
	ProvinceCode *string `json:"province_code"`
	CountryCode  *string `json:"country_code"`

	CreatedAt int64 `json:"created_at"`
}

type StoreListRequest struct {
	Index int `json:"index"`
	Size  int `json:"size"`

	Ids []int64 `json:"ids"`

	Id int64 `json:"id"`

	Name *string `json:"name"`

	Phone *string `json:"phone"`

	Status *int `json:"status"`

	AreaID *int64 `json:"area_id"`

	ZoneCode     *string `json:"zone_code"`
	CityCode     *string `json:"city_code" `
	ProvinceCode *string `json:"province_code"`
	CountryCode  *string `json:"country_code"`
}

type StoreListResponse struct {
	Total int          `json:"total"`
	Data  []*StoreInfo `json:"list"`
}

type StoreInfo struct {
	ID int64 `json:"id"`

	Name string `json:"name"`

	Addr string `json:"addr"`

	Phone string `json:"phone"`

	Coordinate po.Point `json:"coordinate"`

	ManagerID int64 `json:"manager_id"`

	Images pq.StringArray `json:"images"`

	Status int `json:"status"`

	AreaID int64 `json:"area_id"`

	ZoneCode     string `json:"zone_code"`
	CityCode     string `json:"city_code" `
	ProvinceCode string `json:"province_code"`
	CountryCode  string `json:"country_code"`

	OfflineTime int64 `json:"offline_time"`

	CreatedAt int64 `json:"created_at"`

	UpdatedAt int64 `json:"updated_at"`
}

type StoreDeleteRequest struct {
	ID int64 `json:"id" binding:"required"`
}

type StoreBaseRequest struct {
	ID int64 `json:"id" binding:"required"`
}

type CountByAreaResponse struct {
	AreaId int64 `json:"area_id"`
	Count  int64 `json:"count"`
}

type StoreUpdateStatusRequest struct {
	ID     int64 `json:"id" binding:"required"`
	Status *int  `json:"status" binding:"required"`
}

func StoresEntityToDto(stores []*entity.Store) []*StoreInfo {
	out := make([]*StoreInfo, 0, len(stores))
	for _, c := range stores {
		out = append(out, StoreEntityToDto(c))
	}
	return out
}

// NearbyStoresRequest request for nearby stores
type NearbyStoresRequest struct {
	// Coordinate of the user
	Coordinate po.Point `json:"coordinate" binding:"required"`
}

type NearbyStoreInfo struct {
	StoreInfo
	Distance int32 `json:"distance"`
}

type NearbyStoresResponse struct {
	Total int                `json:"total"`
	Data  []*NearbyStoreInfo `json:"list"`
}

func StoreEntityToDto(e *entity.Store) *StoreInfo {
	return &StoreInfo{

		ID: e.ID,

		Name: e.Name,

		Addr: e.Addr,

		Phone: e.Phone,

		Coordinate: e.Coordinate,

		ManagerID: e.ManagerID,

		Images: e.Images,

		Status: e.Status,

		AreaID: e.AreaID,

		ZoneCode:     e.ZoneCode,
		CityCode:     e.CityCode,
		ProvinceCode: e.ProvinceCode,
		CountryCode:  e.CountryCode,

		OfflineTime: e.OfflineTime,

		CreatedAt: e.CreatedAt,

		UpdatedAt: e.UpdatedAt,
	}
}
